package com.stone.boot.stoneboot.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stone.boot.stoneboot.module.serverinfo.entity.ServerInfoDO;
import com.stone.boot.stoneboot.module.serverinfo.mapper.ServerInfoMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * 判断服务的定时任务
 */
@Component
public class JudgingServicesScheduledTasks {

    @Resource
    private ServerInfoMapper serverInfoMapper;

    /**
     * 每5分钟执行一次的定时任务方法
     * 该方法检查特定条件，并在满足条件时打印当前时间
     */
//    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        List<ServerInfoDO> serverInfoList = serverInfoMapper.selectList(new QueryWrapper<>());
        for (ServerInfoDO serverInfoDO : serverInfoList) {
            Date updateTime = serverInfoDO.getUpdateTime();
            Date createTime = serverInfoDO.getCreateTime();
            if (updateTime == null) {
                if (createTime != null) {
                    updateTime = createTime;
                } else {
                    updateTime = new Date();
                    createTime = new Date();
                }
            }
            serverInfoDO.setUpdateTime(updateTime);
            serverInfoDO.setCreateTime(createTime);
            Date date = new Date();
            if (date.getTime() - updateTime.getTime() > 600000) {
                serverInfoDO.setStatus(false);
            } else {
                serverInfoDO.setStatus(true);
            }
            serverInfoMapper.updateById(serverInfoDO);
        }

    }
}
